package com.sff.easyexcel.test.demo.column.width;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.google.common.collect.Lists;
import com.sff.easyexcel.test.demo.util.FilePathUtil;
import lombok.Data;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.util.List;

/**
 * 自适应列宽
 */
public class AdaptColumnWidth {

    private static final short FONT_HEIGHT_IN_POINTS = 11;

    public static void main(String[] args) {

        String writeFileName = FilePathUtil.getPath() + "employee-" + System.currentTimeMillis() + ".xlsx";

        ExcelWriter writer = null;
        try {

            writer = EasyExcel.write(writeFileName, Employee.class)
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .registerWriteHandler(getHorizontalCellStyleStrategy())
                    .build();

            WriteSheet writeSheet = EasyExcel.writerSheet("员工信息").build();
            writer.write(createData(), writeSheet);

        } finally {
            if (writer != null) {
                writer.finish();
            }
        }
    }

    // 设置表头样式、单元格内容自动换行
    public static HorizontalCellStyleStrategy getHorizontalCellStyleStrategy() {
        // excel 表头格式设置
        WriteCellStyle headCellStyle = new WriteCellStyle();
        headCellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());  //蓝绿色

        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints(FONT_HEIGHT_IN_POINTS);
        headWriteFont.setFontName("微软雅黑");
        headWriteFont.setBold(Boolean.TRUE); //字体加粗

        //设置字体
        headCellStyle.setWriteFont(headWriteFont);
        //自动换行
        headCellStyle.setWrapped(Boolean.TRUE);
        //垂直居中
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        //表头边框
        headCellStyle.setBorderLeft(BorderStyle.THIN);
        headCellStyle.setBorderRight(BorderStyle.THIN);

        // excel表格内容样式设置
        WriteCellStyle contentCellStyle = new WriteCellStyle();

        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints(FONT_HEIGHT_IN_POINTS);
        contentWriteFont.setFontName("微软雅黑");
        contentWriteFont.setFontHeightInPoints(FONT_HEIGHT_IN_POINTS);
        contentCellStyle.setWriteFont(contentWriteFont);

        //自动换行
        contentCellStyle.setWrapped(Boolean.TRUE);

        //垂直居中
        contentCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headCellStyle, contentCellStyle);
        return horizontalCellStyleStrategy;
    }


    private static List<Employee> createData() {

        List<Employee> orderList = Lists.newArrayList();
        Employee employee = new Employee();

        employee.setName("张三");
        employee.setDeptName("唐门科技有限公司/技术部/暗器冶炼部");
        employee.setJobName("软件开发工程师");
        orderList.add(employee);

        Employee employee1 = new Employee();
        employee1.setName("李四");
        employee1.setDeptName("唐门科技有限公司/总裁办公室/人力资源部/技术部/AAAAAAAAAAA/BBBBBBBBBBB/CCCCCCCCCCCC/DDDDDDDDDD/EEEEEEEEE/FFFFFFFF/GGGGGGGGGG/HHHHHHHHHHHH");
        employee1.setJobName("CEO");
        orderList.add(employee1);

        return orderList;
    }


    @Data
    private static class Employee {

        @ExcelProperty(value = "姓名", index = 0)
        private String name;

        @ExcelProperty(value = "部门", index = 1)
        private String deptName;

        @ExcelProperty(value = "职位", index = 2)
        private String jobName;
    }

}
